<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/7/15
 * Time: 14:47
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Report extends Base {
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $map =array();
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            if(isset($status)&&$status!="")
            {
                $map['report_status'] = ['like',"%" . $status . "%"];
            }
//            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
//            {
//                $map['a.s_time'] = ['>= time',$start];
//            }
//            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
//            {
//                $map['a.s_time'] = ['<= time',$end];
//            }
//            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
//            {
//                $map['a.s_time'] = ['between time',[$start,$end]];
//            }
            $count = Db::table('up_report')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.r_user_id','left')
                ->where($map)
                ->count();
            $list = Db::table('up_report')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.r_user_id','left')
                ->where($map)
                ->order('report_time desc')
                ->page($page, $limit)
                ->field('a.*,uuu.user_username,uuu.user_phone')
                ->select();//计算总页面
            
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        return $this->fetch();
    }

    //删除
    public function del(){
        $id = input('param.id');
        $info = Db::table('up_report')->where('report_id', $id)->find();
        $res = Db::table('up_report')->where('report_id',$id)->delete();
        if($res){
            $list = [];
            $vo = (string)$info['r_user_id'];
            array_push($list, $vo);
            $list1 = [];
            $vo1 = (string)$info['report_user_id'];
            array_push($list1, $vo1);
            try{
                $this->Jpush(1, $list, '您的举报已被管理员拒绝受理', ['report_type' => $info['report_type'], 'subject_id' => $info['r_type_id']]);
            }catch(\Exception $e){
            }
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    //处理
    public function remark(){
        $id = input('param.id');
        $info = Db::table('up_report')->where('report_id', $id)->find();
        $res = Db::table('up_report')->where('report_id',$id)->setField('report_status',2);
        if($res){
            $list = [];
            $vo = (string)$info['r_user_id'];
            array_push($list, $vo);
            try{
                $this->Jpush(1, $list, '您的举报已被管理员受理', ['report_type' => $info['report_type'], 'subject_id' => $info['r_type_id']]);
            }catch(\Exception $e){
            }
            return json(['code' => 200, 'msg' => '备注成功']);
        }else{
            return json(['code' => 100, 'msg' => '备注失败']);
        }
    }
    
    /**
     * 举报详情
     * @return mixed
     */
    public function reportDetail(){
        $report_id = input('report_id');
        $info = Db::table('up_report')->where('report_id', $report_id)->find();
        $answer_title = '';
        if($info['report_type']==1){//老兵提问
            $answer_title= Db::table('up_old_forum')->where('old_forum_id',$info['r_type_id'])->value('old_forum_title');
        }elseif ($info['report_type']==2){//老兵回答
            $answer_title= Db::table('up_old_answer')->where('old_answer_id',$info['r_type_id'])->value('old_answer_content');
        }elseif ($info['report_type']==3){//悬赏问答
            $answer_title= Db::table('up_answer')->where('answer_id',$info['r_type_id'])->value('answer_title');
        }elseif ($info['report_type']==4){//悬赏回答
            $answer_title= Db::table('up_answer_reply')->where('reply_answer_id',$info['r_type_id'])->value('reply_answer_content');
        }elseif ($info['report_type']==5){
            $answer_title= Db::table('up_love_post')->where('post_id',$info['r_type_id'])->value('post_content');
        }
        $this->assign('answer_title', $answer_title);
        $this->assign('info', $info);
        return $this->fetch('report/detail');
    }
}